import React from 'react'
// ===================================================================== 
const { $fn, $Lazy, $http } = window
// ===================================================================== 
const Modal 		= $Lazy.state(()=>import('@antd/modal'))
const SubmitForm 	= $Lazy.state(()=>import('#cpt/form/submit-form'))
const message 		= $Lazy.file(()=>import('@antd/message'))
// ===================================================================== 
const Index = function( { title, okText, addText='添加', editText='编辑', width=650, minWidth, suffix, height, labelWidth='s6', api, enter=false, editApi, 
	close, param, scroll, refresh=true, onRefresh, getSubmit, onForm, open, onComplete,text, isFlex
}){
	const { ids, submitLoading, formData } = this.state
	let _title = title ? title + ' - ' : ''
	return (
		<Modal
			width 		= { width }
			height 		= { height }
			onRef		= { ref => this.modal = ref }
			title		= { ids ? _title + editText : _title + addText }
			close		= { () => { 
				if(api && editApi){ this.setState({ids:null}) }
				close?.()
			}}
			loading 	= { submitLoading }
			enter		= { enter }
			okText		= { okText }
			minWidth	= { minWidth }
			isFullScroll= { scroll }
			isFlex 		= { isFlex }
			open		= { open }
		>
			<div>
				<SubmitForm
					data 		= { formData }
					labelWidth 	= { labelWidth }
					hideButton
					onForm		= { onForm }
					onSubmit 	= { _param => {
						let _api = api
						let _tit = title ? title :''
						let txt = _tit + addText + '成功'
						let add = true
						if(ids){
							_api = editApi
							_param = {..._param, ...ids}
							// txt = _tit + editText + '成功'
							txt = text ? text: _tit + editText + '成功'
							add = false
							if(!_api){
								return $fn.toast('请传递编辑接口')
							}
						}
						// 地址
						const areaData = _param.areaData ?  $fn.getAddress(_param.areaData) : null
						//动态获取提交参数
						if(getSubmit){
							const submit = getSubmit(_param)
							this.submit = {...this.submit, ...submit}
						}
						_param = {...this?.initSubmit, ...param, ..._param, ...areaData, ...this?.submit }
						$http.submit(this, _api, { param: _param }).then(data=>{
							this.modal.close()
							message.then(f=>f.success(txt))
							if(refresh){
								$fn.delay(this?.fetch)
							}
							onRefresh?.({param:_param, data})
							this.props?.onRefresh?.({param:_param, data})
							onComplete?.({param:_param, data, add })
						})
					}}
					onRef = { ref => this.formRef = ref }
				 />
			</div>
			{ suffix }
		</Modal>
	)
}
export default Index